<!DOCTYPE html>
<!--[if lt IE 7]>      <html class="no-js lt-ie9 lt-ie8 lt-ie7"> <![endif]-->
<!--[if IE 7]>         <html class="no-js lt-ie9 lt-ie8"> <![endif]-->
<!--[if IE 8]>         <html class="no-js lt-ie9"> <![endif]-->
<!--[if gt IE 8]><!-->
<html class="no-js">
<!--<![endif]-->

<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>非父子组件间通信 vue1.x实现</title>
    <meta name="description" content="">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="../style.css">
</head>

<body>
    <!--[if lt IE 7]>
            <p class="browsehappy">You are using an <strong>outdated</strong> browser. Please <a href="#">upgrade your browser</a> to improve your experience.</p>
        <![endif]-->
    <div id="app">
        <p>非父子组件间通信 vue1.x实现</p>
        <span>{{ message }}</span>
        <button @click="handleBroadcast">父组件广播事件给子组件</button>
        <br>
        <my-component></my-component>
    </div>

    <script src='https://cdnjs.cloudflare.com/ajax/libs/vue/1.0.28/vue.min.js'></script>
    <script src="" async defer></script>

    <script>
        Vue.component('my-component', {
            data: function () {
                return {
                    message: '没有收到父组件的广播数据'
                }
            },
            template: '<div class="component">\
                <button @click="handleDispatch">子组件派发事件给父组件</button>\
                <span> {{message}} </span>\
                </div>\
                ',
            methods: {
                handleDispatch: function () {
                    this.$dispatch('on-message', '收到来自子组件的派发数据')
                }
            },
            events: {
                'on-broadcast': function (msg) {
                    this.message = msg
                }
            }
        })

        var app = new Vue({
            el: '#app',
            data: {
                message: '没有收到子组件的派发数据'
            },
            events: {
                'on-message': function (msg) {
                    this.message = msg
                }
            },
            methods: {
                handleBroadcast: function () {
                    this.$broadcast('on-broadcast', '收到来自父组件的广播数据')
                }
            }
        })
    </script>

</body>

</html>